﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace SFJDManagement
{
	class IVehicleColumn : DataGridBoundColumn
	{

		protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
		{
			ClassDataPlus data = dataItem as ClassDataPlus;
			List<ClassVehicle> liVeh = data.车辆及项目;
			StackPanel grid = new StackPanel();
			//grid.SetBinding(grid.bin);

			

			grid.Orientation = Orientation.Horizontal;
			if (liVeh != null && liVeh.Count > 0)
			{
				grid.Children.Add(GetBox(data.字号.ToString()));
				for (int i = 0; i < liVeh.Count; i++)
				{
					grid.Children.Add(new TextBox() { Text = liVeh[i].Number });
					grid.Children.Add(new TextBox() { Text = liVeh[i].Brand });
					grid.Children.Add(new TextBox() { Text = liVeh[i].Type });
				}
			}
			else
			{
				grid.Children.Add(GetBox("无牌照"));
				grid.Children.Add(GetBox("无品牌"));
				grid.Children.Add(GetBox("无型号"));
			}
			return grid;
		}

		protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
		{
			Binding bind = Binding as Binding;	//对grid进行数据绑定
			DataGrid grid = new DataGrid();
			grid.SetBinding(DataGrid.ItemsSourceProperty, bind);
			grid.HeadersVisibility = DataGridHeadersVisibility.Column;
			grid.CanUserAddRows = false;
			
			RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);	//相对绑定，将grid的IsReadOnly属性与父控件的属性绑定
			rs.AncestorType = typeof(DataGrid);
			Binding bind2 = new Binding("IsReadOnly") { RelativeSource = rs };
			grid.SetBinding(DataGrid.IsReadOnlyProperty, new Binding("IsReadOnly") { Path = bind2.Path, Mode = BindingMode.TwoWay, RelativeSource = rs });
			return grid;
		}

		private TextBox GetBox(string str)
		{
			TextBox b = new TextBox();
			b.Text = str;
			b.Margin = new Thickness(0);
			b.BorderThickness = new Thickness(0);
			b.IsReadOnly = true;
			return b;
		}

		private TextBox Box()
		{
			TextBox b = new TextBox();
			Binding bind = new Binding("Number");
			b.SetBinding(TextBox.TextProperty, bind);
			return b;
		}
	}
}
